﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTGroup.Networking.Framework.Utils.WinForm;
using DTGroup.Networking.Framework;
using DTGroup.iNek.WinUI._Codes;
using DTGroup.iNek.WinUI;

namespace DTGroup.LTEPlanDesktop.WinUI.SuperMapForm
{
    public partial class GeoCoordSysForm : Form
    {
        private string geoCoordSysName;

        /// <summary>
        /// 地理坐标系 OID
        /// </summary>
        public string GeoCoordSysName
        {
            get
            {
                if (geoCoordSysName.IsNullOrEmpty() == false)
                {
                    return geoCoordSysName;
                }

                return string.Empty;
            }
        }

        /// <summary>
        /// 是否 选中行
        /// </summary>
        private bool isSelectedRow = false;

        public GeoCoordSysForm()
        {
            InitializeComponent();
            this.dgvGeoCoordSys.AutoGenerateColumns = false;
            this.dgvGeoCoordSys.MultiSelect = false;
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GeoCoordSysForm_Load(object sender, EventArgs e)
        {
            BindingData(string.Empty);

            this.dgvGeoCoordSys.ClearSelection();
        }

        /// <summary>
        /// 确定 按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTrue_Click(object sender, EventArgs e)
        {
            if (dgvGeoCoordSys.SelectedRows.Count > 0)
            {
                DataGridViewRow dr = this.dgvGeoCoordSys.SelectedRows[0];
                this.geoCoordSysName = dr.Cells[1].Value.ToString();

                isSelectedRow = true;

                this.Dispose();
                this.Close();
            }
            else
            {
                MessageBoxUtil.Show(this, "请选择地理坐标系", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 单元格 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvGeoCoordSys_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dgvGeoCoordSys.Rows[e.RowIndex];
                if (row != null)
                {
                    geoCoordSysName = row.Cells[1].Value.ToString();

                    isSelectedRow = true;

                    this.Dispose();
                    this.Close();
                }
            }
        }

        /// <summary>
        /// 窗体 关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GeoCoordSysForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isSelectedRow == false)
            {
                this.geoCoordSysName = string.Empty;
            }
        }

        /// <summary>
        /// 绑定 数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        private void BindingData(string strWhere)
        {
            List<GeoCoordSysEntity> geoCoordSyss = GeoCoordSysManager.Instance.GeoCoordSysCollection;

            if (strWhere.IsNullOrEmpty())
            {
                this.dgvGeoCoordSys.DataSource = geoCoordSyss.OrderBy(o => o.Name).ToList();
            }
            else
            {
                IList<GeoCoordSysEntity> geoCoordSysResults = geoCoordSyss.Where(o => o.Name.ToLower().Contains(strWhere.ToLower())).OrderBy(o => o.Name).ToList();
                this.dgvGeoCoordSys.DataSource = geoCoordSysResults;
            }
        }

        /// <summary>
        /// 数据 检索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSearchText_TextChanged(object sender, EventArgs e)
        {
            if (txtSearchText.Text.IsNullOrEmpty())
            {
                BindingData(string.Empty);
            }
            else
            {
                BindingData(txtSearchText.Text.Trim());
            }
        }

        private void dgvGeoCoordSys_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(System.Drawing.Color.Black))
            {
                e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
            }
        }
    }
}
